package com.ycy.smartcourseselection.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ycy.smartcourseselection.common.ResCodeEnum;
import com.ycy.smartcourseselection.common.ResData;
import com.ycy.smartcourseselection.dto.courseManage.CoManagesDTO;
import com.ycy.smartcourseselection.dto.scoreManage.ScManagesDTO;
import com.ycy.smartcourseselection.entity.CoManage;
import com.ycy.smartcourseselection.entity.Course;
import com.ycy.smartcourseselection.entity.ScoreManage;
import com.ycy.smartcourseselection.exception.CustomException;
import com.ycy.smartcourseselection.mapper.ICoManageMapper;
import com.ycy.smartcourseselection.mapper.IScManageMapper;
import com.ycy.smartcourseselection.utils.CommonUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

@SuppressWarnings("DuplicatedCode")  //禁用重复代码检查(因为跟选课时间管理完全相同)
@Service
public class ScManageService extends ServiceImpl<IScManageMapper, ScoreManage> {

    //判断课程是否正在选课
    public ResData<Boolean> judgeScIsBegin(String courseId) {
        if(StrUtil.isBlank(courseId)) throw new CustomException(ResCodeEnum.BAD_REQUEST,"课程id不能为空");

        QueryWrapper<ScoreManage> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        List<ScoreManage> scManageList = list(wrapper);
        if(scManageList.size() == 0) return ResData.success(false);  //当不存在时，默认为未开始
        if(scManageList.size() > 1) throw new CustomException(ResCodeEnum.BAD_REQUEST,"错误地存在多个该课程管理信息");

        String beginTime = scManageList.get(0).getBeginTime();
        String endTime = scManageList.get(0).getEndTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        String nowTime = sdf.format(new Date());
        if(CommonUtils.compareDate(nowTime, beginTime) > 0 && CommonUtils.compareDate(nowTime, endTime) < 0) return ResData.success(true);
        else return ResData.success(false);
    }

    //修改选课管理(因为添加已经在添加课程时一起添加了)
    public ResData<Boolean> saveScManage(ScoreManage scoreManage) {
        String courseId = scoreManage.getCourseId();
        String beginTime = scoreManage.getBeginTime();
        String endTime = scoreManage.getEndTime();

        if(StrUtil.isBlank(courseId)) throw new CustomException(ResCodeEnum.BAD_REQUEST,"课程id不能为空");
        if(StrUtil.isBlank(beginTime)) throw new CustomException(ResCodeEnum.BAD_REQUEST,"开始时间为空");
        if(StrUtil.isBlank(endTime)) throw new CustomException(ResCodeEnum.BAD_REQUEST,"结束时间不能为空");

        QueryWrapper<ScoreManage> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        boolean isRemove = remove(wrapper);

        return ResData.success(saveOrUpdate(scoreManage));
    }

    //批量开始选课
    public ResData<Boolean> saveBatchScManage(ScManagesDTO scManagesDTO) {
        ArrayList<String> courseIdList = scManagesDTO.getCourseIdList();
        String beginTime = scManagesDTO.getBeginTime();
        String endTime = scManagesDTO.getEndTime();

        if(courseIdList.size() == 0) throw new CustomException(ResCodeEnum.BAD_REQUEST,"课程id数组不能为空");
        if(StrUtil.isBlank(beginTime)) throw new CustomException(ResCodeEnum.BAD_REQUEST,"开始时间为空");
        if(StrUtil.isBlank(endTime)) throw new CustomException(ResCodeEnum.BAD_REQUEST,"结束时间不能为空");

        QueryWrapper<ScoreManage> wrapper = new QueryWrapper<>();
        wrapper.in("course_id",courseIdList);
        List<ScoreManage> list = list(wrapper);
        boolean isRemove = remove(wrapper);

        ArrayList<ScoreManage> addList = new ArrayList<>();
        for (String courseId: courseIdList) {
            ScoreManage scoreManage = new ScoreManage();
            scoreManage.setCourseId(courseId);
            scoreManage.setBeginTime(beginTime);
            scoreManage.setEndTime(endTime);
            addList.add(scoreManage);
        }

        return ResData.success(saveOrUpdateBatch(addList));
    }
}
